package com.android.bonn.fast.drawable.tags;

import android.content.Context;
import android.graphics.Paint;
import android.text.SpannableStringBuilder;
import android.text.Spanned;

import androidx.annotation.ColorInt;
import androidx.annotation.DimenRes;


public class AndroidTagBadgeBuilder {

    private SpannableStringBuilder stringBuilder;
    private final int radius;
    private Context context;

    private int tagTextSizeDimenResource;
    private int tagTextVerticalPaddingDimenResource;
    private int tagTextHorizontalPaddingDimenResource;
    private int iconTextSpacePadding;
    private int tagWhiteSpacePadding;

    public AndroidTagBadgeBuilder(
            Context context,
            int radius,
            @DimenRes int tagTextSize,
            @DimenRes int tagTextVerticalPadding,
            @DimenRes int tagTextHorizontalPadding,
            @DimenRes int iconTextSpacePadding) {

        this.radius = radius;
        this.context = context;
        this.tagTextSizeDimenResource = tagTextSize;
        this.tagTextVerticalPaddingDimenResource = tagTextVerticalPadding;
        this.tagTextHorizontalPaddingDimenResource = tagTextHorizontalPadding;
        this.iconTextSpacePadding = iconTextSpacePadding;
        this.tagWhiteSpacePadding = 5;
    }

    public void setStringBuilder(SpannableStringBuilder stringBuilder) {
        this.stringBuilder = stringBuilder;
    }

    public void appendTag(String tagName, @ColorInt int textColor, @ColorInt int badgeColor, Paint.Style bgStyle) {
        appendTag(tagName, textColor, badgeColor, 0, bgStyle, 1.5f);

    }

    public void appendTag(String tagName, @ColorInt int textColor, @ColorInt int badgeColor, int leftIconDrawableResource, Paint.Style bgStyle, float bgStrokeWidth) {

        //final String nbspSpacing = "\u202F\u202F"; // none-breaking spaces

        /*String badgeText = nbspSpacing + tagName + nbspSpacing;*/
        stringBuilder.append(tagName);

        RoundedTagSpan tagSpan = new RoundedTagSpan(
                context,
                radius,
                badgeColor,
                textColor,
                tagTextSizeDimenResource,
                tagTextVerticalPaddingDimenResource,
                tagTextHorizontalPaddingDimenResource,
                iconTextSpacePadding,
                tagWhiteSpacePadding,
                leftIconDrawableResource,
                bgStyle,
                bgStrokeWidth);

        stringBuilder.setSpan(
                tagSpan,
                stringBuilder.length() - tagName.length(),
                stringBuilder.length() - tagName.length() + tagName.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        stringBuilder.append(" ");

    }

    public void clear() {
        stringBuilder.clear();
        stringBuilder.clearSpans();
    }
}